﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DataObjectsPhone;
using System.Device.Location;
using Microsoft.Maps.MapControl;
using System.Windows.Navigation;
namespace ee_wp_tourmaker.Views
{
    public partial class NewCheckPointPage : PhoneApplicationPage
    {

        double lati = 0.0;
        double longi = 0.0;

        public NewCheckPointPage()
        {
            InitializeComponent();

            ListPickerItem i1 = new ListPickerItem();
            i1.Content = "Up to 50 meters";
            ListPickerItem i2 = new ListPickerItem();
            i2.Content = "Up to 1km";
            ListPickerItem i3 = new ListPickerItem();
            i3.Content = "Up to 5km";
            accuracyListPicker.Items.Add(i1);
            accuracyListPicker.Items.Add(i2);
            accuracyListPicker.Items.Add(i3);
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
           if (App.MainModel.SelectedCheckPoint.Longitude != (Decimal)0.0)
            {
                lati = (double)App.MainModel.SelectedCheckPoint.Latitude;
                longi = (double)App.MainModel.SelectedCheckPoint.Longitude;
                longitudeTextBlock.Text = "Longitude: " + longi;
                latitudeTextBlock.Text = "Latitude: " + lati;
            }
        }
        private void ApplicationBarSaveButton_Click(object sender, EventArgs e)
        {
            CheckPoint checkpoint = new CheckPoint();
            checkpoint.Name = nameTextBox.Text;
            checkpoint.Description = descTextBox.Text;
            checkpoint.Latitude = (Decimal)lati;
            checkpoint.Longitude = (Decimal)longi;
            checkpoint.Points = 2;
            switch (accuracyListPicker.SelectedIndex)
            {
                case 0:
                    checkpoint.Accuracy = 100;
                    break;
                case 1:
                    checkpoint.Accuracy = 1000;
                    break;
                case 2:
                    checkpoint.Accuracy = 5000;
                    break;
            }

            if (checkpoint.Name.Length != 0 && checkpoint.Latitude != (Decimal)0.0)
            {
                App.MainModel.newChallenge.checkpointsList.Add(checkpoint);
                NavigationService.GoBack();
            }
            else
            {
                MessageBox.Show("You must select a point on the map and a name for your checkpoint!");
            }
        }

        private void ApplicationBarCancelButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void openMap_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/NewCheckPointMapPage.xaml", UriKind.Relative));
        }
    }
}